package apps.web.service;

import java.util.List;
import java.util.Map;

import webframe.common.helper.AppConstant;
import webframe.common.helper.FormatTransformHelper;
import apps.web.model.Apply;
import apps.web.model.ApplyCensor;
import apps.web.model.Decl;
import apps.web.model.Goods;
import apps.web.model.GoodsClearItem;
import apps.web.model.PassageBill;

public class ApplyBizService extends MyBaseService {
	public String findMaxApplyCode() {
		int p = AppConstant.getAppPropValue("applyCodePrefix").length() + 1;
		Map map = getJdbcDao().findOneRecord(
				"select max(substr(apply_code," + p + ")) code from apply");
		return (String) map.get("code");
	}

	public Apply findApplyById(Long id) {
		return getApplyDao().get(id);
	}

	public List<Apply> findApplyByApplyCode(String applyCode) {
		List<Apply> list = getApplyDao().findByHql("from Apply t where t.applyCode=?",
				new Object[] { applyCode });
		return list;
	}

	public List<Apply> findApplyByExhName(String exhName) {
		List<Apply> list = getApplyDao().findByHql("from Apply t where t.exhName=?",
				new Object[] { exhName });
		return list;
	}

	public List<ApplyCensor> findApplyCensorByApplyId(Long applyId) {
		List<ApplyCensor> list = getApplyCensorDao().findByHql(
				"from ApplyCensor t where t.applyId=?", new Object[] { applyId });
		return list;
	}

	public List<Decl> findDeclByApplyId(Long applyId) {
		List<Decl> list = getDeclDao().findByHql(
				"from Decl t where t.applyId=? order by t.declDate", new Object[] { applyId });
		return list;
	}

	public String findDeclByApplyIdAndMonitorCondition(Long applyId, String monStr) {
		String sql = "select distinct t.decl_no as d from goods t where t.apply_id=" + applyId;
		if (monStr != null && monStr.length() != 0) {
			sql = sql + " and t.insp_monitor_cond like '%" + monStr + "%' order by decl_no asc";
		} else {
			sql = sql
					+ " and (t.insp_monitor_cond is null or t.insp_monitor_cond='') order by decl_no asc";
		}

		StringBuffer sb = new StringBuffer();
		List<Map> list = getJdbcDao().findRecordList(sql);
		for (Map map : list) {
			sb.append(FormatTransformHelper.SEG);
			sb.append((String) map.get("d"));
		}
		String re = "";
		if (sb.length() > 0) {
			re = sb.toString().substring(1);
		}
		return re;
	}

	public List<Goods> findClearGoodsByApplyIdAnd(Long applyId) {
		String sql = "from Goods t where t.applyId=? and t.anchor=1 order by t.declNo,t.goodsNo";
		List<Goods> list = getGoodsDao().findByHql(sql, new Object[] { applyId });
		for (Goods goods : list) {
			GoodsClearItem gci = getGoodsClearItemDao().findSingleByHql(
					"from GoodsClearItem t where t.declNo=? and t.goodsNo=? ",
					new Object[] { goods.getDeclNo(), goods.getGoodsNo() });
			if (gci != null) {
				List<PassageBill> passageBills = getPassageBillDao().findByHql(
						"from PassageBill t where t.declNo=? and t.goodsNo=?",
						new Object[] { gci.getDeclNo(), gci.getGoodsNo() });
				if (passageBills != null && passageBills.size() != 0) {
					gci.setPassageBills(passageBills);
				}
				goods.setGoodsClearItem(gci);
			}
		}
		return list;
	}

	// //////////////////////////
	public void updateApply(Apply entity) {
		getApplyDao().update(entity);
	}

	public void deleteApply(Long id) {
		Apply apply = findApplyById(id);
		apply.setDelFlag(1L);
		getApplyDao().update(apply);
	}

	public void saveApplyCensor(ApplyCensor entity) {
		getApplyCensorDao().save(entity);
	}

	public Apply saveApply(Apply entity) {
		return getApplyDao().save(entity);
	}
}
